import './style.css'

import 'reflect-metadata'
import { DataSource } from 'typeorm'

import initSqlJs from 'sql.js'
import localforage from 'localforage'
import wasm from "sql.js/dist/sql-wasm.wasm?url";
import { isObject, isString } from '@vue/shared';
import { Find } from './Find'

import * as entities from './entities'


window.localforage = localforage

const SQL = await initSqlJs({
  locateFile: (file: string) => wasm,
})


const ds = new DataSource({
  type: 'sqljs',
  driver: SQL,
  autoSave: true,
  // logging: ['info', 'log', 'query'],
  location: "example_db",
  useLocalForage: true,
  synchronize: true,

  entities: Object.values(entities),
})

await ds.initialize()

// await ds.manager.upsert('user', { id: '1', name: 'xxxx' }, ['id'])
// await ds.manager.upsert('user', { id: '2', name: 'hhhh' }, ['id'])

// await ds.manager.upsert('post', { id: '1', title: 'xxx', content: 'content', user: { id: '1' } }, ['id'])
// await ds.manager.upsert('post', { id: '2', title: '222', content: 'content', user: { id: '1' } }, ['id'])
// await ds.manager.upsert('post', { id: '3', title: '333', content: 'content', user: { id: '2' } }, ['id'])
// await ds.manager.upsert('post', { id: '4', title: '444', content: 'content', user: { id: '1' } }, ['id'])
// await ds.manager.upsert('post', { id: '5', title: '555', content: 'content', user: { id: '1' } }, ['id'])
// await ds.manager.upsert('post', { id: '5ew', title: 'asfr555', content: 'content', user: { id: '2' } }, ['id'])
// await ds.manager.upsert('post', { id: 'post-tag-test', title: 'post-tag-test', content: 'post-tag-test', user: { id: '1' } }, ['id'])

// await ds.manager.upsert('tag', { id: '1', name: 'class1' }, ['id'])
// await ds.manager.upsert('tag', { id: '2', name: 'class2' }, ['id'])
// await ds.manager.upsert('tag', { id: '3', name: 'class3' }, ['id'])

// await ds.manager.createQueryBuilder().relation('post', 'tags').of('2').remove('1')
// await ds.manager.createQueryBuilder().relation('post', 'tags').of('2').add('1')
// await ds.manager.createQueryBuilder().relation('post', 'tags').of('2').remove('2')
// await ds.manager.createQueryBuilder().relation('post', 'tags').of('2').add('2')


// console.log(await ds.createQueryBuilder().insert().into('post').values({ title: '' }).getInsertedColumns())
console.log(await ds.createQueryRunner().query(`INSERT or ignore into post (id, title) values ('wxxx', 'xxx') returning *`));
// console.log(ds.getMetadata('post').findRelationWithPropertyPath('tags')!.junctionEntityMetadata)


console.log(
  '\n',
  await new Find(ds, 'post', {
    select: { id: true, title: true, user: { select: { id: true } } },
    orderBy: {
      title: 'desc',
      user: { name: 'desc' },
      tags: { _count: 'asc' }
    },
    where: {
      user: {
        name: 'xxxx'
      }
    }
  })
  .getSql(),
  // .findMany(),
  '\n',
  await new Find(ds, 'user', {
    select: { id: true, name: true, posts: { select: { id: true, title: true, user: { select: { name: true } } } } },
    orderBy: {
      title: 'desc',
      posts: { _count: 'desc' }
    }
  })
  // .getSql(),
  .findMany(),
)


  

// console.log(And(LessThan(1), MoreThan(1)));


// const uint8Array = ds.sqljsManager.exportDatabase()
// const blob = new Blob([uint8Array], { type: 'application/octet-stream' })
// const a = document.createElement('a')
// a.href = window.URL.createObjectURL(blob)
// a.download = 'sqlite.db'
// a.click()
// window.URL.revokeObjectURL(a.href)

